package main

import (
	"GORM_Demo04/part08/MyPojo"
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

/*	常用方法

	【18】Count:计数
	【19】GROUP：进行分组
	【20】Having:分组后进行过滤

*/

func main() {
	db, err := gorm.Open("mysql", "root:123456@tcp(192.168.11.189:3306)/lztest?charset=utf8&parseTime=True&loc=Local")

	if err != nil {
		panic(err)
	}
	defer db.Close()

	db.LogMode(true)

	/*
		【18】Count:计数
		【19】GROUP：进行分组
		【20】Having:分组后进行过滤*/

	//【18】Count:计数

	/*var users []MyPojo.User
	var count int
	db.Find(&users).Count(&count)
	fmt.Println(users)
	fmt.Println(count)*/

	//【19】GROUP：进行分组
	/*	var users []MyPojo.User
		var group_date []GroupData
		db.Select("u_age as age,count(*) as count").Group("u_age").Find(&users).Scan(&group_date)
		fmt.Println(group_date)*/

	//【20】Having:分组后进行过滤

	var users []MyPojo.User
	var group_date []GroupData

	//SELECT u_age as age,count(*) as count FROM `users`   GROUP BY u_age HAVING (u_age > 33)
	db.Select("u_age as age,count(*) as count").Group("u_age").Having("u_age > ?", 33).Find(&users).Scan(&group_date)
	fmt.Println(group_date)
}

type GroupData struct {
	Age   int
	Count int
}
